﻿import QtQuick 2.0
import "./components"

Item {

    id: root

    height: parent.height
    width: parent.width

    property bool contentEditable: gExpIsRunning === 0

    // 更新实验名称的model
    function updateExpModel(){
        var currentModel = combo_mainMethod.getText()
        combo_mainMethod.model = CGetMessageInterface.getMainMethodList()
        combo_mainMethod.setIndex(combo_mainMethod.model.indexOf(currentModel))
    }

    Component.onCompleted: {
        gStateExp.initExpInfo()
        state2Edit()
    }

    function state2Edit(){
        combo_mainMethod.setText(gStateExp.mainMethod)
        combo_consumableName.setText(gStateExp.consumableName)
        edit_equName1.setText(gStateExp.equName)
        edit_equName2.setText(gStateExp.equName)
        edit_expName.setText(gStateExp.expName)
        edit_code.setText(gStateExp.consumableCode)
        edit_memo.setText(gStateExp.memo)
        edit_expCategories.setText(gStateExp.expCategories)
//        edit_date.setText(gStateExp.execDate)
        edit_operator1.setText(gStateExp.operator)
        edit_operator2.setText(gStateExp.operator)
        updateExpModel()
        refreshConsumbaleCodeState()

    }

    onVisibleChanged: {
        if(visible){
            state2Edit()
        }
    }

    function refreshConsumbaleCodeState(){
        gStateExp.checkConsumableCode(edit_code.getText())
        edit_code.redBorder = edit_code.getText() === "" ? false : !gStateExp.consumableCodeStatus
        edit_code.redInfo = edit_code.getText() === "" ? "" : gStateExp.consumableCodePromptMsg
        if(gExpIsRunning === 1){
            edit_code.redBorder = false
            edit_code.redInfo = false
        }
    }

    // 中间内容外面又一圈带透明度的
    Rectangle{
        anchors.centerIn: layout_center
        height: layout_center.height+80*dp
        width: layout_center.width+80*dp
        radius: 5*dp
        color: Qt.rgba(1, 1, 1, 0.5)
    }
    // 中间内容
	Rectangle{
        id: layout_center
        y: 12*dp
        anchors.centerIn: parent
        height: 680*dp; width: 1280*dp
        radius: 5*dp


        Column{
            x: 30*dp; y: 20*dp
            width: parent.width - 2*x
            spacing: 20*dp
            Item{           // 标题行 ============================================
                height: 40*dp; width: parent.width
                Text {
                    text: qsTr("实验信息")
                    font.pixelSize: config.fontsize_1
                    font.bold: true
                    anchors.verticalCenter: parent.verticalCenter
                }
                Row{
                    x: parent.width - width
                    spacing: 10*dp
//                    CustomButton{
//                        title: "一键清洗"
//                        defaultMode1: true
//                        onClicked: {
//                        }
//                    }
                    CustomButton{
                        title: "复位原点"
                        enabled: gExpIsRunning === 0
                        button_icon: "qrc:/picture/sjx.png"
                        defaultMode1: true
                        onClicked: {
                            if(gConnectStatus === false){
                                box_error_common.box_text = "请先连接设备"
                                box_error_common.show()
                                return
                            }
                            box_opResetOrigin.show()
                        }

                        CustomPopupBoxWarning{
                            id: box_opResetOrigin
                            box_text: "确认复位设备到原点位置吗?"
                            onAccepted: {
                                box_resetting.infoState = 0
                                CMaintenance.reset("0", "0", "")
                                CAnimationShifting.reset()
                                CAnimationPractice.reset()
                                CAnimationPurify.reset()
                                box_resetting.show()        // 显示等待复位的弹窗
                            }
                        }
                    }

                    CustomButton{
                        title: "实验复位"
                        button_icon: "qrc:/picture/sjx.png"
                        defaultMode1: true
                        enabled: gExpIsRunning === 0
                        onClicked: {
                            if(gConnectStatus === false){
                                box_error_common.box_text = "请先连接设备"
                                box_error_common.show()
                                return
                            }
                            if(gStateExp.selectedExpName === ""){
                                box_error_common.box_text = "请先选择实验"
                                box_error_common.show()
                                return
                            }
                            if(gStateExp.consumableName === "" && gAppModule!==1){
                                box_error_common.box_text = "请先选择纯化耗材"
                                box_error_common.show()
                                return
                            }
                            box_opResetAll.show()
                        }

                        CustomPopupBoxWarning{
                            id: box_opResetAll
                            box_text: "确认复位设备到【"+gStateExp.selectedExpName+"】实验位置吗?"
                            onAccepted: {
                                box_resetting.infoState = 0
                                // 全部复位
                                if(gStateExp.selectedExpType === 0){
                                    CAnimationShifting.reset()
                                    CAnimationPractice.reset()
                                    CAnimationPurify.reset()
                                    CMaintenance.reset("1", "0", gStateExp.consumableName)
                                }

                                box_resetting.show()        // 显示等待复位的弹窗
                            }
                        }
                    }
                    CustomButton{
                        title: "打印报告"
                        button_icon: "qrc:/picture/x_dybg.png"
                        defaultMode1: true
                        onClicked: {
                            box_printer.close()
                            box_printer.show()
                        }
                    }
                    CustomButton{
                        title: "系统设置"
                        button_icon: "qrc:/picture/phsz.png"
                        defaultMode1: true
                        enabled: gExpIsRunning === 0 || (gExpIsFailure !== 0)
                        onClicked: {
                            preset.show()
                        }
                    }
//                    CustomButton{
//                        title: "下一步"
//                        button_icon: "qrc:/picture/xyb.png"
//                        defaultMode1: true
//                        onClicked: {        // 校验主页实验名称必填
//                            if(edit_expName.getText() === ""){
//                                box_error_common.box_text = "实验名称不能为空"
//                                box_error_common.show()
//                                return
//                            }
//                            gPageIdx = 1
//                        }
//                    }
                }

            }
            Rectangle{      // 实验信息内容 =====================================
                id: layout_content
                height: 500*dp; width: parent.width
                radius: 5*dp
                border.color: "#44444444"
                property real editWidth: 300*dp
                // 左侧 ------------------------------------------------------->
                Item {
                    id: layout_left
                    property real titleWidth: 110*dp
                    height: parent.height; width: parent.width * 0.5
                    Column{
                        x: 20*dp; y: 20*dp
                        width: parent.width - 2*x
                        spacing: 20*dp
                        Text {
                            text: qsTr('<font color="#FF0000"">*</font>' + "注：请选择项目名称进行实验，如没有项目名称，请点击系统设置-项目配置中设置")
                            color: "#999999"
                            font.pixelSize: config.fontsize * 0.98
                        }
                        CustomComboBox{
                            id: combo_mainMethod
                            title: "项目名称"

                            anchors.horizontalCenter: parent.horizontalCenter
                            titleWidth: layout_left.titleWidth; editWidth: layout_content.editWidth
                            enabled: root.contentEditable
                            showClearButton: true
                            onClickText: {
                                gStateExp.mainMethod = text
                                if(index===-1){         // 被清空
                                    gStateExp.clearExpSelected()
                                }
                                state2Edit()
                            }onPopupVisibleChanged: {
                                state2Edit()
                            }
                        }CustomComboBox{
                            id: combo_consumableName
                            title: qsTr("纯化耗材名称")

                            enabled: false
                            anchors.horizontalCenter: parent.horizontalCenter
                            visible: gAppModule === 0 || gAppModule === 2               // 屏蔽模块
                            titleWidth: layout_left.titleWidth; editWidth: layout_content.editWidth
                            onClickText: {
                                gStateExp.consumableName = text
                                state2Edit()
                            }
                        }
                        CustomInputText{
                            id: edit_code
                            title: qsTr("耗材编码")

                            visible: gAppModule === 0 || gAppModule === 2               // 屏蔽模块
                            titleWidth: layout_left.titleWidth; editWidth: layout_content.editWidth
                            anchors.horizontalCenter: parent.horizontalCenter
                            editable: root.contentEditable
                            onTextChanged: {
                                gStateExp.consumableCode = getText()

                                // 判断是否有不合法字符
                                var res = true
                                for(let i = 0; i < getText().length; i ++){
                                    console.log(config.validChar.indexOf(getText()[i])<0)
                                    if(config.validChar.indexOf(getText()[i])<0){
                                        res = false
                                        break
                                    }
                                }
                                console.log(res)
                                if(!res){
                                    redBorder = true
                                    redInfo = "请输入英文、数字、下划线"
                                }else{
                                    redBorder = false
                                }
                            }
                            onEnterPressed: {
                                refreshConsumbaleCodeState()
                            }onEditingFinished: {
                                refreshConsumbaleCodeState()
                            }
                            inputMaxLength: 20
                            Text{
                                text: gStateExp.consumableCodeStatus ? "有效" : "无效"
                                color: gStateExp.consumableCodeStatus ? "green" : "red"
                                height: 28*dp; width: 48*dp
                                verticalAlignment: Text.AlignVCenter
                                anchors.verticalCenter: parent.verticalCenter
                                x: parent.width - width - 5*dp
                                font.pixelSize: config.fontsize
                                MouseArea{
                                    cursorShape: "PointingHandCursor"
                                    anchors.fill: parent
                                    onClicked: {
                                        console.log("点击耗材编码")
                                    }
                                }
                            }
                        }
                        CustomInputText{            // 左边的实验类别
                            id: edit_expCategories
                            title: "实验类别"
                            inputMaxLength: 30
                            editable: root.contentEditable
                            titleWidth: layout_left.titleWidth; editWidth: layout_content.editWidth
                            anchors.horizontalCenter: parent.horizontalCenter
                            visible: gAppModule === 0 || gAppModule === 2               // 屏蔽模块
                            onTextChanged: {
                                gStateExp.expCategories = getText()
                                state2Edit()
                            }
                        }

                        // 你猜我为什么同样的输入框要放两个 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                        CustomInputText{            // 左边的设备名称
                            id: edit_equName1
                            title: qsTr("设备名称")

                            inputMaxLength: 30
                            titleWidth: layout_left.titleWidth; editWidth: layout_content.editWidth
                            anchors.horizontalCenter: parent.horizontalCenter
                            editable: false
                            visible: gAppModule===1
                            onTextChanged: {
                                gStateExp.equName = getText()
                                state2Edit()
                            }
                        }
                        CustomInputText{            // 左边的操作人
                            id: edit_operator1
                            title: "操作人"
                            inputMaxLength: 30
                            editable: root.contentEditable
                            titleWidth: layout_left.titleWidth; editWidth: layout_content.editWidth
                            anchors.horizontalCenter: parent.horizontalCenter
                            visible: gAppModule===1
                            onTextChanged: {
                                gStateExp.operator = getText()
                                state2Edit()
                            }
                        }
                        // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                        Item{
                            height: 40*dp; width: layout_content.editWidth + layout_left.titleWidth
                            anchors.horizontalCenter: parent.horizontalCenter
                            CustomButton{
                                id: button_confirm
                                title: "更换纯化耗材"
                                defaultMode1: true
                                visible: gAppModule === 0 || gAppModule === 2               // 屏蔽模块
                                width: 120*dp
                                x: parent.width - width
                                enabled: root.contentEditable
                                onClicked: {
//                                    gStateExp.confirmExp()
                                    gStateExp.confirmConsumableName1()
                                }
                            }
//                            CustomButton{
//                                id: button_clear
//                                title: "清空"
//                                defaultMode2: true
//                                width: 90*dp
//                                enabled: root.contentEditable
//                                x: button_confirm.x - width - 10*dp
//                                onClicked: {
//                                    gStateExp.clearExpSelected()
//                                    state2Edit()
//                                }
//                            }
                        }
                    }
                }
                Rectangle{ height: parent.height; width: 1; color: "#44444444"; x: parent.width*0.5; visible: gAppModule===0 }     // 间隔线
                // 右侧 --------------------------------------------------------->
                Item {
                    id: layout_right
                    property real titleWidth: 80*dp
                    x: parent.width*0.5
                    height: parent.height; width: parent.width * 0.5
                    Column{
//                        anchors.centerIn: parent
                        anchors.horizontalCenter: parent.horizontalCenter
                        y: 60*dp
                        spacing: 20*dp
                        CustomInputText{
                            id: edit_expName
                            title: qsTr("名称")

                            required: true
                            inputMaxLength: 30
                            titleWidth: layout_right.titleWidth; editWidth: layout_content.editWidth
                            onTextChanged: {
                                gStateExp.expName = getText()
                                state2Edit()
                            }
                            editable: root.contentEditable
                        }
                        CustomInputText{            // 右边的设备名称
                            id: edit_equName2
                            title: qsTr("设备名称")

                            inputMaxLength: 30
                            titleWidth: layout_right.titleWidth; editWidth: layout_content.editWidth
                            editable: false
                            visible: gAppModule===0
                            onTextChanged: {
                                gStateExp.equName = getText()
                                state2Edit()
                            }
                        }
                        CustomComboCalendarGrid{
                            id: edit_date
                            title: "实验日期"

                            enabled: root.contentEditable
                            titleWidth: layout_right.titleWidth; editWidth: layout_content.editWidth
                            onDateChanged: {
                                gStateExp.execDate = getText()
                                state2Edit()
                            }
                        }
                        CustomInputText{            // 右边的操作人
                            id: edit_operator2
                            title: "操作人"
                            visible: gAppModule===0

                            inputMaxLength: 30
                            editable: root.contentEditable
                            titleWidth: layout_right.titleWidth; editWidth: layout_content.editWidth
                            onTextChanged: {
                                gStateExp.operator = getText()
                                state2Edit()
                            }
                        }

                        CustomInputText{
                            id: edit_memo
                            title: "描述"

                            titleWidth: layout_right.titleWidth; editWidth: layout_content.editWidth
                            editHeight: 80*dp
                            inputMaxLength: 120
                            editable: root.contentEditable
                            editWrapModel: 1
                            onTextChanged: {
                                gStateExp.consumableCode = getText()
                            }
                        }
                    }
                }
            }
            Item{           // 下面的注释 =======================================
                height: 40*dp; width: parent.width
                Text{
                    text:  '<font color="#FF0000"">*</font>' + "注：如进行单个模块的实验，请到该实验下运行实验，另进行单个模块的实验时，此处项目名称应该清空"
                    font.pixelSize: config.fontsize
                }
            }
        }
    }


}
